package likou;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/10/13 08:01
 */
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class _24 {
    public static void main(String[] args) {

    }

    public static ListNode swapPairs(ListNode head) {
        ListNode daan;
        ListNode temp = head;
        int num = 0;
        while (temp != null) {
            num++;
            temp = temp.next;
        }
        num /= 2;
        if (num < 1) {
            return head;
        }
        ListNode now = head;
        ListNode next = head.next;
        now.next = next.next;
        next.next = now;
        ListNode front = now;
        daan = next;
        Deque<ListNode> deque = new ArrayDeque<>();
        num--;
        while (num > 0) {
            now = now.next;
            next = now.next;
            now.next = next.next;
            next.next = now;
            front.next = next;
            front = now;
            num--;
        }
        return daan;
    }
}
